package com.settlement.system.controller;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.BrandUnit;
import com.settlement.system.model.entity.common.TblWpBrandUnitConfig;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.IBrandUnitService;
import com.settlement.system.service.ITblWpBrandUnitConfigService;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 唯品会品牌对应配置 前端控制器
 * @author xiaochuan
 * @since 2020-04-09
 */
@Slf4j
@RestController
@RequestMapping("/wp/brandUnitConfig")
public class TblWpBrandUnitConfigController extends ExcelController<TblWpBrandUnitConfig> {

    @Resource
    private ITblWpBrandUnitConfigService tblWpBrandUnitConfigService;

    @Resource
    private IBrandUnitService brandUnitService;

    @Override
    public IBaseService init() {
        return tblWpBrandUnitConfigService;
    }


    @Override
    public Class<?> getModelClass() {
        return TblWpBrandUnitConfig.class;
    }

    @Override
    protected void setModelValue(TblWpBrandUnitConfig tblWpBrandUnitConfig) throws Exception {
        BrandUnit brandUnit = null;
        try {
            Map<String ,Object> params = new HashMap<>(16);
            params.put("nameShort",tblWpBrandUnitConfig.getNameShort());
            brandUnit = (BrandUnit)brandUnitService.selectByParam(params).get(0);
            tblWpBrandUnitConfig.setBrandUnitNo(brandUnit.getBrandUnitNo());
            tblWpBrandUnitConfig.setBrandUnitName(brandUnit.getName());
        } catch (ServiceException e) {
            throw new Exception(e);
        }
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        String nameShortList = (String)model.get("nameShortList");
        model.put("nameShortList",FasUtil.formatInQueryCondition(nameShortList));
    }

    @Override
    //@CacheEvict(cacheNames = {"wpBrandMap"},allEntries = true, beforeInvocation = true)
    public ObjectDataResponse importFile(MultipartFile file, HttpServletRequest request) {
        return super.importFile(file, request);
    }

    @Override
    //@CacheEvict(cacheNames = {"wpBrandMap"}, allEntries = true, beforeInvocation = true)
    public ObjectDataResponse update(@RequestBody TblWpBrandUnitConfig model, HttpServletRequest request) throws ServiceException {
        return super.update(model, request);
    }

    @Override
    //@CacheEvict(cacheNames = {"wpBrandMap"}, allEntries = true, beforeInvocation = true)
    public ObjectDataResponse delete(@RequestBody TblWpBrandUnitConfig model, HttpServletRequest request) throws ServiceException {
        return super.delete(model, request);
    }

    @Override
    //@CacheEvict(cacheNames = {"wpBrandMap"}, allEntries = true, beforeInvocation = true)
    public ObjectDataResponse insert(@RequestBody TblWpBrandUnitConfig model, HttpServletRequest request) throws ServiceException {
        return super.insert(model, request);
    }

    @Override
    public String verifyImportObject(TblWpBrandUnitConfig tblWpBrandUnitConfig, AnalysisContext analysisContext) {

        String message = super.verifyImportObject(tblWpBrandUnitConfig, analysisContext);
        if(StringUtils.isNotEmpty(message)){
            return message;
        }

        try {
            Map<String ,Object> params = new HashMap<>(16);
            params.put("nameShort",tblWpBrandUnitConfig.getNameShort());
            List<BrandUnit> brandUnits = brandUnitService.selectByParam(params);
            if(CollectionUtils.isEmpty(brandUnits)){
                return "品牌简称不存在";
            }
            BrandUnit brandUnit = brandUnits.get(0);
            tblWpBrandUnitConfig.setBrandUnitNo(brandUnit.getBrandUnitNo());
            if(!tblWpBrandUnitConfig.getBrandUnitName().equals(brandUnit.getName())){
                return "品牌部名称和品牌简称不匹配";
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            throw new RuntimeException(e);
        }

        return null;
    }
}

